#include <stdio.h>
#include <stdlib.h>
#include "../../lib/util/util.h"
#include "../../app/gconstant.h"


void arr_char_pcpy(char* dst, int dst_start, char* src, int src_len) {
    int i;
    for(i = 0;i < src_len;i++) {
        dst[i + dst_start] = src[i];
    }
}


void prt_arr_char(char* arr, int len, int is_key_val, char* arr_name, int hex) {
    printf("%s \n",arr_name);
    int i;
    for(i = 0;i < len; i++) {
        if(is_key_val) {
            switch(hex) {
                case HEX_02X:
                    printf("%d => %02X \n", i, arr[i]);
                    break;
                case HEX_c:
                    printf("%d => %c \n", i, arr[i]);
                    break;
                default:
                    break;
            }

        } else {
            switch(hex) {
                case HEX_02X:
                    printf("%X \n", arr[i]);
                    break;
                case HEX_c:
                    printf("%c \n", arr[i]);
                    break;
                default:
                    break;
            }
        }
    }
}


int system_sh(char* sh_str) {
    printf("system_sh <<<--- \n");

    int status = system(sh_str);
    if (status == -1) {
        printf("system_sh <<<--- system error ! status = %d \n", status);
        return 0;
    }
    if (!WIFEXITED(status)) {
        printf("system_sh <<<--- exit status = %d \n", WEXITSTATUS(status));
        return 0;
    }
    if (WEXITSTATUS(status) != 0) {
        printf("system_sh <<<--- run shell script fail script exit status = %d \n", WEXITSTATUS(status));
        return 0;
    }

    printf("system_sh --->>> \n");
    return 1;
}


